﻿IF OBJECT_ID(N'__EFMigrationsHistory') IS NULL
BEGIN
    CREATE TABLE [__EFMigrationsHistory] (
        [MigrationId] nvarchar(150) NOT NULL,
        [ProductVersion] nvarchar(32) NOT NULL,
        CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
    );
END;

GO

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20171225074932_v1_Initialization')
BEGIN
    CREATE TABLE [ContentComments] (
        [Id] uniqueidentifier NOT NULL,
        [AuthorId] bigint NOT NULL,
        [AuthorUserName] nvarchar(30) NOT NULL,
        [Body] nvarchar(max) NOT NULL,
        [ContentId] bigint NOT NULL,
        [PostedTime] datetime2 NOT NULL,
        CONSTRAINT [PK_ContentComments] PRIMARY KEY ([Id])
    );
END;

GO

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20171225074932_v1_Initialization')
BEGIN
    CREATE TABLE [Contents] (
        [Id] bigint NOT NULL IDENTITY,
        [AuthorId] bigint NOT NULL,
        [AuthorUserName] nvarchar(30) NOT NULL,
        [Body] nvarchar(max) NOT NULL,
        [Channel] int NOT NULL,
        [PostedTime] datetime2 NOT NULL,
        [Status] int NOT NULL,
        [Subject] nvarchar(60) NOT NULL,
        [VisitNum] int NOT NULL,
        CONSTRAINT [PK_Contents] PRIMARY KEY ([Id])
    );
END;

GO

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20171225074932_v1_Initialization')
BEGIN
    CREATE TABLE [PointsStrategies] (
        [Id] nvarchar(20) NOT NULL,
        [ExpPoints] int NOT NULL,
        [Name] nvarchar(30) NOT NULL,
        [TradingPoints] int NOT NULL,
        CONSTRAINT [PK_PointsStrategies] PRIMARY KEY ([Id])
    );
END;

GO

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20171225074932_v1_Initialization')
BEGIN
    CREATE TABLE [UserPointList] (
        [Id] uniqueidentifier NOT NULL,
        [CreatedTime] datetime2 NOT NULL,
        [Exp] int NOT NULL,
        [PointStrategyId] nvarchar(20) NOT NULL,
        [Remark] nvarchar(100) NULL,
        [TradingPoints] int NOT NULL,
        [UserId] bigint NOT NULL,
        CONSTRAINT [PK_UserPointList] PRIMARY KEY ([Id])
    );
END;

GO

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20171225074932_v1_Initialization')
BEGIN
    CREATE TABLE [Users] (
        [Id] bigint NOT NULL IDENTITY,
        [AvatarUrl] nvarchar(150) NULL,
        [Email] nvarchar(300) NULL,
        [EmailConfirmed] bit NOT NULL,
        [ExpMaxPoints] bigint NOT NULL,
        [ExpPoints] bigint NOT NULL,
        [LastLoginTime] datetime2 NOT NULL,
        [Level] int NOT NULL,
        [Mobile] nvarchar(16) NULL,
        [MobileConfirmed] bit NOT NULL,
        [Password] nvarchar(128) NOT NULL,
        [PasswordSalt] nvarchar(128) NOT NULL,
        [SecurityToken] nvarchar(max) NULL,
        [Status] int NOT NULL,
        [TradingPoints] int NOT NULL,
        [UserName] nvarchar(30) NOT NULL,
        CONSTRAINT [PK_Users] PRIMARY KEY ([Id])
    );
END;

GO

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20171225074932_v1_Initialization')
BEGIN
    CREATE UNIQUE INDEX [IX_Users_Email] ON [Users] ([Email]) WHERE [Email] IS NOT NULL;
END;

GO

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20171225074932_v1_Initialization')
BEGIN
    CREATE UNIQUE INDEX [IX_Users_Mobile] ON [Users] ([Mobile]) WHERE [Mobile] IS NOT NULL;
END;

GO

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20171225074932_v1_Initialization')
BEGIN
    CREATE UNIQUE INDEX [IX_Users_UserName] ON [Users] ([UserName]);
END;

GO

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20171225074932_v1_Initialization')
BEGIN
    INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20171225074932_v1_Initialization', N'2.0.1-rtm-125');
END;

GO

